import {defineStore} from 'pinia'
import persistLocalstorage from "@/common/persistLocalstorage";

interface BluetoothPrinter {
  connected: boolean,
  printerName: string,
  deviceId: string,
  printDelayTime: number,
  writeCharaterId: string,
  writeServiceId: string,
  notifyCharaterId: string,
  notifyServiceId: string,
  readCharaterId: string,
  readServiceId: string,
}

interface NetworkPrinter {
  connected: boolean,
  printerName: "",
  host: string,
}

interface DirectPrinter {
  connected: boolean,
  printerName: string,
  host: string,
}

interface PrintTimes {
  customerReceipt: number,
  pickupReceipt: number,
  merchantReceipt: number,
}

interface OrderGoodsPrintSortType {
  customerReceipt: number,
  pickupReceipt: number,
  merchantReceipt: number,
}

interface Printer {
  printerType: 'bluetoothPrinter' | 'networkPrinter' | 'directPrinter',
  autoPrint: boolean,
  bluetoothPrinter: BluetoothPrinter,
  networkPrinter: NetworkPrinter,
  directPrinter: DirectPrinter,
  printTimes: PrintTimes,
  orderGoodsPrintSortType: OrderGoodsPrintSortType
}

export const usePrinterStore = defineStore('printerStore', {
  state: (): Printer => ({
    printerType: 'bluetoothPrinter',
    autoPrint: false,
    //蓝牙信息
    bluetoothPrinter: {
      connected: false,
      printerName: "",
      deviceId: "",
      writeCharaterId: "",
      writeServiceId: "",
      notifyCharaterId: "",
      notifyServiceId: "",
      readCharaterId: "",
      readServiceId: "",
      printDelayTime: 20,
    },
    networkPrinter: {
      connected: false,
      printerName: "",
      host: ''
    },
    directPrinter: {
      connected: false,
      printerName: "",
      host: ''
    },
    printTimes: {
      customerReceipt: 1,
      pickupReceipt: 0,
      merchantReceipt: 1,
    },
    orderGoodsPrintSortType: {
      customerReceipt: 1,
      pickupReceipt: 1,
      merchantReceipt: 1,
    }
  }),
  actions: {
    setAutoPrint(autoPrint: boolean) {
      this.autoPrint = autoPrint;
    },
    setPrinterType(printerType: Printer['printerType']) {
      this.printerType = printerType;
    },
    setBluetoothPrinter(bluetoothPrinter: BluetoothPrinter) {
      this.bluetoothPrinter = bluetoothPrinter;
      plus.storage.setItem('thisPrintDeviceId', bluetoothPrinter.deviceId);
      plus.storage.setItem('writeServiceId', bluetoothPrinter.writeServiceId);
      plus.storage.setItem('writeCharacteristicId', bluetoothPrinter.writeCharaterId);
    },
    setNetworkPrinter(networkPrinter: NetworkPrinter) {
      this.networkPrinter = networkPrinter;
    },
    setDirectPrinter(directPrinter: DirectPrinter) {
      this.directPrinter = directPrinter;
    },
    setPrintTimes(printTimes: PrintTimes) {
      this.printTimes = printTimes;
    },
  },
  persist: {
    storage: persistLocalstorage
  }
});